package puzzle.game.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import puzzle.game.BallPuzzle;
import puzzle.game.states.LoadingState;
import puzzle.game.states.PuzzleState;
import puzzle.util.ResourceManager;
import puzzle.util.TimerAction;

public class ExitState extends PuzzleState {
  
  public static int ID = 6;
  
  protected String exitStr = ResourceManager.getString("game.msg.exit",
                                        "Thanks for playing!");
  private Color col = new Color(193, 228, 248);
  private TimerAction timer = new TimerAction(2000);
  private boolean fading = false;
  
  public ExitState(BallPuzzle bp) {
    super(bp);
  }
  
  public void initState(GameContainer c) throws SlickException {
    setBackground(Color.white);
    timer.setRepeats(false);
  }
  
  public void startState(GameContainer c) throws SlickException {
    timer.restart();
  }
  
  public void renderState(GameContainer c, Graphics g) {
    Font f = getContext().getLoadingState().getMainFont();
    g.setFont(f);
    
    int x = c.getWidth()/2 - f.getWidth(exitStr)/2;
    int y = c.getHeight()/2 - f.getHeight(exitStr)/2;
    
    g.setColor(col);
    g.drawString(exitStr, x, y);
  }
  
  public void updateState(GameContainer c, int delta) {
    if (timer.update(delta)) {
      fading = true;
    }
    
    if (fading) {
      col.a -= delta * 0.002f;
      if (col.a <= 0) {
        col.a = 0;
        c.exit();
      }
    }
  }
  
  public int getID() {
    return ID;
  }
}
